﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmBaccap : Form
    {
        public FrmBaccap()
        {
            InitializeComponent();
          
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab == tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Baccapdau;
            }
            else
            {
                if (tabControl2.SelectedTab == tabControl2.TabPages[1])
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.Baccapcanh;
                }
                else
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.Baccapsan;
                }
            }
        }

        private void FrmBaccap_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmBaccap_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(606, 510);
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrBn prBn = new PrBn();
            prBn.Show();
            this.Hide();
        }

        private void FrmBaccap_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
        //Save Setting vào Registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Tuong dau
            Values = txtTDau_DayMTr.Text; ;//0
            Values = Values + "\t" + txtTDau_DayMSau.Text ;//1
            Values = Values + "\t" + txtTDau_Day.Text ;//2
            Values = Values + "\t" + txtTDau_doc.Text ;//3
            Values = Values + "\t" + txtBn_GocMoTren.Text ;//4
            Values = Values + "\t" + txtTDau_mrongS.Text ;//5
            //Bac nuoc
            Values = Values + "\t" + txtBn_DayDinh.Text ;//6
            Values = Values + "\t" + txtBn_Day.Text ;//7
            Values = Values + "\t" + txtBn_DayDem.Text ;//8
            Values = Values + "\t" + txtHtn_DayDra.Text ;//9
            Values = Values + "\t" + txtHtn_DayDvao.Text ;//10
            Values = Values + "\t" + txtHtn_DayMong.Text ;//11
            Values = Values + "\t" + txtHtn_Rong.Text ;//12
            //Tcanh co
            Values = Values + "\t" + txtHtn_SauDra.Text ;//13
            Values = Values + "\t" + txtHtn_SauDvao.Text ;//14
            Values = Values + "\t" + txtTCanh_CaoCuoi.Text ;//15
            Values = Values + "\t" + txtTCanh_CaoGiua.Text ;//16
            Values = Values + "\t" + txtTCanh_CaoChan.Text ;//17
            Values = Values + "\t" + txtTCanh_Day.Text ;//18
            Values = Values + "\t" + txtTCanh_Morong.Text ;//19
            Values = Values + "\t" + txtTCanh_MorongM.Text ;//20
            Values = Values + "\t" + txtTCanh_DocN.Text ;//21

            Values = Values + "\t" + txtRonggotieunang.Text;
            Values = Values + "\t" + txtCaogotieunang.Text;
            Values = Values + "\t" + txtDaigotieunang.Text; 
            Values = Values + "\t" + txtCkgotieunang.Text;
            Values = Values + "\t" + txtRongroda.Text;
            Values = Values + "\t" + txtCaoroda.Text; 
            Values = Values + "\t" + txtDairoda.Text;
            Values = Values + "\t" + ChcBtn.Checked.ToString();
            Values = Values + "\t" + ChcGtn.Checked.ToString();
            Values = Values + "\t" + ChcRd.Checked.ToString();          
            regKey.SetValue("DimBaccap",Values);
            regKey.Close();
           
        }

        // Load Setting từ Registry
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey!=null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("DimBaccap").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                txtTDau_DayMTr.Text = STR[i++];//0
                txtTDau_DayMSau.Text = STR[i++];//1
                txtTDau_Day.Text = STR[i++];//2
                txtTDau_doc.Text = STR[i++];//3
                txtBn_GocMoTren.Text = STR[i++];//4
                txtTDau_mrongS.Text = STR[i++];//5
                //Bac nuoc
                txtBn_DayDinh.Text = STR[i++];//6
                txtBn_Day.Text = STR[i++];//7
                txtBn_DayDem.Text = STR[i++];//8
                txtHtn_DayDra.Text = STR[i++];//9
                txtHtn_DayDvao.Text = STR[i++];//10
                txtHtn_DayMong.Text = STR[i++];//11
                txtHtn_Rong.Text = STR[i++];//12
                //Tcanh co
                txtHtn_SauDra.Text = STR[i++];//13
                txtHtn_SauDvao.Text = STR[i++];//14
                txtTCanh_CaoCuoi.Text = STR[i++];//15
                txtTCanh_CaoGiua.Text = STR[i++];//16
                txtTCanh_CaoChan.Text = STR[i++];//17
                txtTCanh_Day.Text = STR[i++];//18
                txtTCanh_Morong.Text = STR[i++];//19
                txtTCanh_MorongM.Text = STR[i++];//20
                txtTCanh_DocN.Text = STR[i++];//21

                txtRonggotieunang.Text = STR[i++];
                txtCaogotieunang.Text = STR[i++];
                txtDaigotieunang.Text = STR[i++];
                txtCkgotieunang.Text = STR[i++];
                txtRongroda.Text = STR[i++];
                txtCaoroda.Text = STR[i++];
                txtDairoda.Text = STR[i++];
                ChcBtn.Checked = bool.Parse(STR[i++]);
                ChcGtn.Checked = bool.Parse(STR[i++]);
                ChcRd.Checked = bool.Parse(STR[i++]);
                regKey.Close();
            }
        }
        // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.bac)|*.bac";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Tuong dau
                Str = "------TUONG DAU------" + "\r\n";
                Str = Str + "Chieu cao H1:" + "\t" + txtTDau_cao.Text + "\r\n";
                Str = Str + "Chieu cao H2:" + "\t" + txtTDau_DayMTr.Text + "\r\n";
                Str = Str + "Chieu cao H3:" + "\t" + txtTDau_DayMSau.Text + "\r\n";
                Str = Str + "Chieu cao H4:" + "\t" + txtBn_DayDem.Text + "\r\n";
                Str = Str + "Be day b1:" + "\t" + txtTDau_Day.Text + "\r\n";
                Str = Str + "Be day b2:" + "\t" + txtTDau_mrongS.Text + "\r\n";
                Str = Str + "Nghieng lung tuong i:" + "\t" + txtTDau_doc.Text + "\r\n";
                Str = Str + "Rong tuong B:" + "\t" + txtTDau_rong.Text + "\r\n";
                //Bac cap
                Str = Str + "------BAC CAP------" + "\r\n";
                Str = Str + "Cao tuong H1:" + "\t" + txtTCanh_CaoDinh.Text + "\r\n";
                Str = Str + "Cao tuong H2:" + "\t" + txtTCanh_CaoGiua.Text + "\r\n";
                Str = Str + "Cao tuong H3:" + "\t" + txtTCanh_CaoChan.Text + "\r\n";
                Str = Str + "Day bac H4:" + "\t" + txtBn_DayDinh.Text + "\r\n";
                Str = Str + "Day bac H5:" + "\t" + txtBn_Day.Text + "\r\n";
                Str = Str + "Chan khay b:" + "\t" + txtHtn_DayDvao.Text + "\r\n";
                Str = Str + "Chan khay H:" + "\t" + txtHtn_SauDvao.Text + "\r\n";
                Str = Str + "Rong b1:" + "\t" + txtTCanh_Morong.Text + "\r\n";
                Str = Str + "Rong b2:" + "\t" + txtBn_Rong.Text + "\r\n";
                Str = Str + "Day b3:" + "\t" + txtTCanh_Day.Text + "\r\n";
                Str = Str + "Day b4:" + "\t" + txtTCanh_MorongM.Text + "\r\n";
                Str = Str + "Goc mo tren:" + "\t" + txtBn_GocMoTren.Text + "\r\n";
                Str = Str + "Goc mo duoi:" + "\t" + txtBn_GocMoDuoi.Text + "\r\n";
                Str = Str + "Chieu dai mo goc:" + "\t" + txtTcanh_Daimogoc.Text + "\r\n";
                Str = Str + "Nghieng tuong canh:" + "\t" + txtTCanh_DocN.Text + "\r\n";
                Str = Str + "Rong cua vao B:" + "\t" + txtRongB.Text + "\r\n";
                //Tieu nang
                Str = Str + "------TIEU NANG------" + "\r\n";
                Str = Str + "Co be tieu nang?:" + "\t" + ChcBtn.Checked.ToString() + "\r\n";
                Str = Str + "Cao thanh H1:" + "\t" + txtHtn_SauDra.Text + "\r\n";
                Str = Str + "Cao thanh H2:" + "\t" + txtTCanh_CaoCuoi.Text + "\r\n";
                Str = Str + "Day long H3:" + "\t" + txtHtn_DayMong.Text + "\r\n";
                Str = Str + "Dai b1:" + "\t" + txtHtn_Rong.Text + "\r\n";
                Str = Str + "Day b2:" + "\t" + txtHtn_DayDra.Text + "\r\n";
                Str = Str + "Co go tieu nang?:" + "\t" + ChcGtn.Checked.ToString() + "\r\n";
                Str = Str + "Rong go:" + "\t" + txtRonggotieunang.Text + "\r\n";
                Str = Str + "Cao go:" + "\t" + txtCaogotieunang.Text + "\r\n";
                Str = Str + "Dai go:" + "\t" + txtDaigotieunang.Text + "\r\n";
                Str = Str + "Cach khoang go:" + "\t" + txtCkgotieunang.Text + "\r\n";
                Str = Str + "Co ro da?:" + "\t" + ChcRd.Checked.ToString() + "\r\n";
                Str = Str + "Rong ro da:" + "\t" + txtRongroda.Text + "\r\n";
                Str = Str + "Cao ro da:" + "\t" + txtCaoroda.Text + "\r\n";
                Str = Str + "Dai ro da:" + "\t" + txtDairoda.Text + "\r\n";
                Str = Str + "So hang:" + "\t" + txtHangRoda.Text;

                //Ghi file dữ liệu
                FileStream fs=new FileStream(filename,FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }

        // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.bac)|*.bac";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog()==DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Tường đầu
                txtTDau_cao.Text = Values[i].Split('\t')[1]; i++;
                txtTDau_DayMTr.Text = Values[i].Split('\t')[1]; i++;
                txtTDau_DayMSau.Text = Values[i].Split('\t')[1]; i++;
                txtBn_DayDem.Text = Values[i].Split('\t')[1]; i++;
                txtTDau_Day.Text = Values[i].Split('\t')[1]; i++;
                txtTDau_mrongS.Text = Values[i].Split('\t')[1]; i++;
                txtTDau_doc.Text = Values[i].Split('\t')[1]; i++;
                txtTDau_rong.Text = Values[i].Split('\t')[1]; i++;
                //Bac cap
                i = i + 1;
                txtTCanh_CaoDinh.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_CaoGiua.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_CaoChan.Text = Values[i].Split('\t')[1]; i++;
                txtBn_DayDinh.Text = Values[i].Split('\t')[1]; i++;
                txtBn_Day.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_DayDvao.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_SauDvao.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_Morong.Text = Values[i].Split('\t')[1]; i++;
                txtBn_Rong.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_Day.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_MorongM.Text = Values[i].Split('\t')[1]; i++;
                txtBn_GocMoTren.Text = Values[i].Split('\t')[1]; i++;
                txtBn_GocMoDuoi.Text = Values[i].Split('\t')[1]; i++;
                txtTcanh_Daimogoc.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_DocN.Text = Values[i].Split('\t')[1]; i++;
                txtRongB.Text = Values[i].Split('\t')[1]; i++;
                //Tieu nang
                i = i + 1;
                ChcBtn.Checked = bool.Parse(Values[i].Substring(Values[i].IndexOf('\t'))); i++;
                txtHtn_SauDra.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_CaoCuoi.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_DayMong.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_Rong.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_DayDra.Text = Values[i].Split('\t')[1]; i++;
                ChcGtn.Checked = bool.Parse(Values[i].Substring(Values[i].IndexOf('\t'))); i++;
                txtRonggotieunang.Text = Values[i].Split('\t')[1]; i++;
                txtCaogotieunang.Text = Values[i].Split('\t')[1]; i++;
                txtDaigotieunang.Text = Values[i].Split('\t')[1]; i++;
                txtCkgotieunang.Text = Values[i].Split('\t')[1]; i++;
                ChcRd.Checked = bool.Parse(Values[i].Substring(Values[i].IndexOf('\t'))); i++;
                txtRongroda.Text = Values[i].Split('\t')[1]; i++;
                txtCaoroda.Text = Values[i].Split('\t')[1]; i++;
                txtDairoda.Text = Values[i].Split('\t')[1]; i++;
                txtHangRoda.Text = Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");

            }
        }
        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }
        
        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmBaccap_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
